// 项目组成员
var projectuser = []
// 子项目列表
var projectchild = []

// 循环添加项目组成员
function bannerbtm() {
    var redgreen = document.querySelectorAll(".redgreen")
    // console.log(redgreen);
    for (var i = 0; i < redgreen.length; i++) {
        if (redgreen[i].innerText === "空闲") {
            redgreen[i].style.color = "#5FC29E"
        } else {
            redgreen[i].style.color = "red"
        }
    }
}


// 子项目列表
var tb = document.querySelector("tbody")
var pageSize = 5; //每页显示的记录条数
var page = 1; //当前页，默认第一页
function footers() {
    var index = localStorage.getItem("projectId")
    var num = Number(index)
    let childrenlis = {
        projectId: num,
        pageindex: page,
        pagenum: pageSize
    }
    ajaxHttp("post", "project/projectDetails", childrenlis).then((res) => {
        res = JSON.parse(res)
        console.log(res);
        var data = res.dataset.childProjectList.projectList
        projectchild = data
        for (var i = 0; i < projectchild.length; i++) {
            var tr = document.createElement("tr")
            tr.className = "trs"
            tr.innerHTML = `
            <td>${projectchild[i].childProjectId}</td>
            <td>${projectchild[i].childProjectName}</td>
            <td>${projectchild[i].childProjectLeader}</td>
            <td>${projectchild[i].workingHours}</td>
            <td>${projectchild[i].startDate}</td>
            <td>${projectchild[i].endDate}</td>
            <td class="state">${projectchild[i].prop}</td>
            <td class="operate">
                <button onclick="subproject(${projectchild[i].childProjectId})" style="color:#2B6FDF;">详情</button>
            </td>
        `
            tb.appendChild(tr)
        }
        var banner_bottom = document.querySelector(".banner-bottom")
        var users=res.dataset.memberList.members
        projectuser=users
        for (var i = 0; i < projectuser.length; i++) {
            var newspan = document.createElement("span")
            newspan.innerHTML = `
                <aside>${projectuser[i].userName}</aside>
                <aside>${projectuser[i].userPhone}</aside>
                <aside class="redgreen">${projectuser[i].status}</aside>
            `
            banner_bottom.appendChild(newspan)
        }
        bannerbtm()
    })

}
footers()

// 跳转到子项目详情
function subproject(rev) {
    localStorage.setItem("childProjectId",rev)
    window.location.href = "Subproject.html"
}

// 鼠标划入添加显示添加子项目和添加项目组成员列表
function showlis() {

    shows.style.display = "block"
}
function hidelis() {
    shows.style.display = "none"
    // console.log(1);
}
var shows = document.querySelector(".shows")
console.log(shows);
var lis = shows.querySelectorAll("li")
console.log(lis);
// 鼠标移入li事件
function details() {
    for (var i = 0; i < lis.length; i++) {
        lis[i].setAttribute("index", i)
        lis[i].onmouseenter = function () {
            var index = this.getAttribute("index")
            for (var j = 0; j < lis.length; j++) {
                lis[j].className = ""
            }
            lis[index].className = "details"
        }
    }
}
details()


// 分页
var theUL = document.getElementById("pageListTableBody");
// console.log(theUL);
var totalPage = document.getElementById("spanTotalPage");
var pageNum = document.getElementById("spanPageNum"); //获取当前页<span>
var spanPre = document.getElementById("spanPre"); //获取上一页<span>
var spanNext = document.getElementById("spanNext"); //获取下一页<span>
var spanFirst = document.getElementById("spanFirst"); //获取第一页<span>
var spanLast = document.getElementById("spanLast"); //获取最后一页<span>
var numberRowsInTable = theUL.getElementsByTagName("tr").length; //记录总条数
console.log(numberRowsInTable);

//下一页
function next() {
    hideTable();
    currentRow = pageSize * page;
    maxRow = currentRow + pageSize;
    if (maxRow > numberRowsInTable) maxRow = numberRowsInTable;
    for (var i = currentRow; i < maxRow; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    page++;
    if (maxRow == numberRowsInTable) {
        nextText();
        lastText();
    }
    showPage();
    preLink();
    firstLink();
}

//上一页
function pre() {
    hideTable();
    page--;
    currentRow = pageSize * page;
    maxRow = currentRow - pageSize;
    if (currentRow > numberRowsInTable) currentRow = numberRowsInTable;
    for (var i = maxRow; i < currentRow; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    if (maxRow == 0) {
        preText();
        firstText();
    }
    showPage();
    nextLink();
    lastLink();
}

//第一页
function first() {
    hideTable();
    page = 1;
    for (var i = 0; i < pageSize; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    firstText();
    preText();
    nextLink();
    lastLink();
}

//最后一页
function last() {
    hideTable();
    page = pageCount();
    currentRow = pageSize * (page - 1);
    for (var i = currentRow; i < numberRowsInTable; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    preLink();
    nextText();
    firstLink();
    lastText();
}

function hideTable() {
    for (var i = 0; i < numberRowsInTable; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
}

function showPage() {
    pageNum.innerHTML = page;
}

//总共页数
function pageCount() {
    return Math.ceil(numberRowsInTable / pageSize);
}
//显示链接
function preLink() {
    spanPre.innerHTML = "<a href='javascript:pre();'>上一页</a>";
}
function preText() {
    spanPre.innerHTML = "上一页";
}
function nextLink() {
    spanNext.innerHTML = "<a href='javascript:next();'>下一页</a>";
}
function nextText() {
    spanNext.innerHTML = "下一页";
}
function firstLink() {
    spanFirst.innerHTML = "<a href='javascript:first();'>首页</a>";
}
function firstText() {
    spanFirst.innerHTML = "首页";
}
function lastLink() {
    spanLast.innerHTML = "<a href='javascript:last();'>末页</a>";
}
function lastText() {
    spanLast.innerHTML = "末页";
}
//隐藏
function hide() {
    for (var i = pageSize; i < numberRowsInTable; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
    totalPage.innerHTML = pageCount();
    pageNum.innerHTML = '1';
    nextLink();
    lastLink();
}
hide();